﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;    
using System.Xml;
using PloobsEngine.SceneControl;
using PloobsEngine.Cameras;
using PloobsEngine.Modelo;

namespace PloobsEngine.Physics.Bepu
{

    public class FaceCameraFollowerGhost : BepuEntityObject
    {
        public FaceCameraFollowerGhost(ICamera cam, Vector3 pos) : base(0)
        {
            this.cam = cam;
            this.pos = pos;
        }

        public FaceCameraFollowerGhost(IObject obj, Vector3 displacement, ICamera cam) : base(0)
        {
            this.cam = cam;
            this.obj = obj;
            this.displacement = displacement;
        }

        private Vector3 displacement = Vector3.Zero;
        private IObject obj;
        ICamera cam;

        public ICamera Camera
        {
            get { return cam; }
            set { cam = value; }
        }
        private Matrix deltarotation = Matrix.Identity;
        private Vector3 pos;
        private Vector3 positionDisplacement = Vector3.UnitY * 2;
        private Vector3? objectForward = null;
        private Vector3 rotationAxis = Vector3.UnitY;

        public Vector3 RotationAxis
        {
            get { return rotationAxis; }
            set { rotationAxis = value; }
        }

        public Vector3? ObjectForward
        {
            get { return objectForward; }
            set { objectForward = value; }
        }

        public Vector3 PositionDisplacement
        {
            get { return positionDisplacement; }
            set { positionDisplacement = value; }
        }

        public override Matrix WorldMatrix
        {
            get
            {
                Vector3 dir = cam.Target - cam.Position;
                dir.Normalize();
                //Matrix m = Matrix.CreateBillboard(pos, cam.Position, cam.Up, dir);
                return Matrix.CreateScale(scale) * Matrix.CreateRotationY(MathHelper.ToRadians(90)) * Matrix.CreateConstrainedBillboard(obj.Position + displacement + pos + positionDisplacement, cam.Position, rotationAxis, dir, objectForward);
            }
        }

        public override Vector3 Position
        {
            get
            {
                return pos;
            }
            set
            {
                this.pos = value;
            }
        }

        public override Vector3 Scale
        {
            get
            {
                return scale;
            }
            set
            {
                this.scale = value;
            }
        }

        public override BoundingBox BoundingBox
        {
            get
            {
                return new BoundingBox();
            }
        }
        public override bool isMotionLess
        {
            get
            {
                return true;
            }
            set
            {
                throw new Exception("Eh sempre movel");
            }
        }
        
        public override Vector3 Velocity
        {
            get
            {
                return Vector3.Zero;
            }
            set
            {
                throw new Exception("Sempre imovel");
            }
        }

        public override void ApplyImpulse(Vector3 force)
        {
            
        }

        public override void ApplyTorque(Vector3 force)
        {
            
        }

        public override void DeSerialize(XmlNode node, EngineStuff engine, IModelo model, Vector3 position, Matrix rotation)
        {
            throw new Exception("NOT IMPLEMENTED YET");
            //this.Position = SerializerHelper.DeserializeVector3("Position", node);
            //this.Rotation = SerializerHelper.DeSerializeMatrix("Rotation", node);            
            //this.Scale = SerializerHelper.DeserializeVector3("Scale", node);
            //Vector3 bmax = SerializerHelper.DeserializeVector3("bbMIN", node);
            //Vector3 bmmin = SerializerHelper.DeserializeVector3("bbMAX", node);
            //bb = new BoundingBox(bmmin, bmax);
        }

        public override void Serialize(System.Xml.XmlTextWriter textWriter)
        {
            throw new Exception("NOT IMPLEMENTED YET");
            //SerializerHelper.SerializeVector3(Scale, "Scale", textWriter);
            //SerializerHelper.SerializeVector3(Position, "Position", textWriter);                        
            //SerializerHelper.SerializeMatrix(Rotation, "Rotation", textWriter);
            //SerializerHelper.SerializeVector3(bb.Max, "bbMAX", textWriter);
            //SerializerHelper.SerializeVector3(bb.Min, "bbMIN", textWriter);            
        }

        public override PhysicObjectTypes PhysicObjectTypes
        {
            get { return PhysicObjectTypes.GHOST; }
        }

    }



}